package com.coolwen.service;

import com.coolwen.dao.RoleMapper;
import com.coolwen.dao.RoleResourceMapper;
import com.coolwen.entity.Resource;
import com.coolwen.entity.Role;
import com.coolwen.entity.RoleResource;
import com.coolwen.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by user on 2016/6/8.
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleMapper roleDao;

    @Autowired
    private RoleResourceMapper rrDao;


    @Override
    public void add(Role role) {
        roleDao.insertSelective(role);
    }

    @Override
    public void delete(long id) {
        roleDao.deleteByPrimaryKey(id);
    }

    @Override
    public Role load(long id) {
        return roleDao.selectByPrimaryKey(id);
    }


    @Override
    public List<Role> listRole() {
        return roleDao.listRole();
        //      return null;
    }

    @Override
    public void update(Role role) {
        roleDao.updateByPrimaryKeySelective(role);
    }


    @Override
    public UserRole loadUserRole(long uid, long roleId) {
        return roleDao.loadUserRole(uid, roleId);
    }

    @Override
    public void addRoleResource(long roleId, long resId) {
        RoleResource rr = new RoleResource();
        rr.setRoleid(roleId);
        rr.setResid(resId);
        rrDao.insertSelective(rr);

    }


    /**
     * 删除某个用户的所有角色
     *
     * @param uid
     */
    @Override
    public void deleteUserRoles(long uid) {
        roleDao.deleteUserRoles(uid);
    }

    /**
     * 根据角色id获取可以访问的所有资源
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Resource> listRoleResource(long roleId) {
        return roleDao.listRoleResource(roleId);
    }


    @Override
    public int deleteRoleResource(long roleId, long resId) {
        return rrDao.deleteByRR(roleId, resId);

    }


}
